package com.wbs.login;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.wbs.model.TbAdmin;
import com.wbs.model.TbSeller;
import com.wbs.resutil.Result;
import com.wbs.resutil.ResultEnums;
import com.wbs.userToken.UserEunm;
import com.wbs.userToken.UserToken;
import com.wbs.utils.JwtUtil;
import com.wbs.vo.AdminVo;
import com.wbs.vo.SellerVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 芝梦
 * @Description: 登陆控制层, 控制 是访问 运营商 还是 商家
 * @DateTime: 2024/1/26 14:41
 */
@RequestMapping("loginController")
@RestController
public class LoginController {
    @Autowired
    private JwtUtil jwtUtil;


    //校验运营商登陆
    @PostMapping("adminLogin")
    public Result adminLogin(@RequestParam String username, @RequestParam String password){
        if (StrUtil.isEmpty(username)||StrUtil.isEmpty(password)){
            return Result.RESPONSE(ResultEnums.USERNAME_AND_PASSWORD_NOT_NULL);
        }
        //1. 创建subject
        Subject subject = SecurityUtils.getSubject();
        //2. 封装token
//        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
        UserToken userToken = new UserToken(username, password, UserEunm.ADMIN);

        //3. 调用login方法  跳入 UserRealm 的 AuthenticationInfo() 身份验证信息
        //身份验证
        try {
            subject.login(userToken);
        } catch (UnknownAccountException e) {
            //用于在控制台打印异常的堆栈跟踪
//            e.printStackTrace();
            return Result.RESPONSE(ResultEnums.USERNAME_AND_PASSWORD_ERROR);
        } catch (IncorrectCredentialsException e){
            return Result.RESPONSE(ResultEnums.USERNAME_AND_PASSWORD_ERROR);
        }
        //获取当前登录成功的用户
        TbAdmin principal = (TbAdmin) subject.getPrincipal();
        //脱敏处理
        AdminVo adminVo = new AdminVo();
        BeanUtils.copyProperties(principal,adminVo);
        //校验成功  生成token
        String token = jwtUtil.createJwt(adminVo.getId().toString(), JSONObject.toJSONString(adminVo), "adminRoles");

//        return Result.SUCCESS().setData(token);
        return Result.RESPONSE(ResultEnums.LOGIN_SUCCESS).setData(token);
    }


    //校验商家登陆
    @PostMapping("sellerLogin")
    public Result sellerLogin(@RequestParam String username, @RequestParam String password){
        //1. 创建subject
        Subject subject = SecurityUtils.getSubject();
        //2. 封装token
        UserToken userToken = new UserToken(username, password, UserEunm.SELLER);

        //3. 调用login方法 跳入UserRealm
        //身份验证
        try {
            subject.login(userToken);
        } catch (UnknownAccountException e) {
            //用于在控制台打印异常的堆栈跟踪
//            e.printStackTrace();
            return Result.RESPONSE(ResultEnums.USERNAME_AND_PASSWORD_ERROR);
        } catch (IncorrectCredentialsException e){
            return Result.RESPONSE(ResultEnums.USERNAME_AND_PASSWORD_ERROR);
        }
        //获取当前登录成功的用户
        TbSeller seller = (TbSeller) subject.getPrincipal();
        //脱敏处理
        SellerVo sellerVo = new SellerVo();
        BeanUtils.copyProperties(seller,sellerVo);
        //校验成功  生成token
        String token = jwtUtil.createJwt(sellerVo.getSellerId(), JSONObject.toJSONString(sellerVo), "adminRoles");

        return Result.RESPONSE(ResultEnums.LOGIN_SUCCESS).setData(token);
    }
}
